import { fetchHome } from '../../services/home/home';
import { fetchGoodsList } from '../../services/good/fetchGoods';

const { statusBarHeight } = wx.getWindowInfo();
Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    swiperImageProps: { mode: 'scaleToFill' },
    statusBarHeight: statusBarHeight,
    classifyList: [
      {
        icon: 'http://120.41.36.48:62800/bugatti/attach/file/preview?id=cf5c96f8e86d7b48675b8f473492b45c',
        path: '/pages/brand/category/index',
        name: "品牌展示",
        type: 1
      },
      {
        icon: 'http://120.41.36.48:62800/bugatti/attach/file/preview?id=9dd2f0a4511dfd3fdd451551a5a9dd8f',
        path: '/pages/brand/category/index',
        name: "报价信息",
        type: 2
      },
      {
        icon: 'http://120.41.36.48:62800/bugatti/attach/file/preview?id=798bb1697458d5cfddd77705ec0f29d5',
        path: '/pages/newsList/newsList?type=1',
        name: "资讯动态"
      },
      {
        icon: 'http://120.41.36.48:62800/bugatti/attach/file/preview?id=cbbffcdf3c0774bf78bb1da6292f7610',
        path: '/pages/newsList/newsList?type=2',
        name: "工程信息"
      },
      {
        icon: 'http://120.41.36.48:62800/bugatti/attach/file/preview?id=1b528d9d80d057d8ed42987f14fbd0f3',
        path: "/pages/eBook/index",
        name: "电子书"
      },
    ],
    zixunList: [
      {
        name: '中国建材集团与苏州市、苏州实验室签署战略合作协议',
        day: '09',
        time: '2025/11',
        type: 'isTop',
      },
      {
        name: '晋江六大建材专业市场积极备战2025年福建建博会',
        day: '09',
        time: '2025/12',
        type: 'isNew',
      },
      {
        name: '标准提升引领建材业发展',
        day: '09',
        time: '2025/9',
        type: '',
      }
    ],
    pinpaiList: [
      {
        id: 1,
        name: '管件管材1',
        itemList: [
          {
            id: 1,
            name: '富华线缆1',
          },
          {
            id: 2,
            name: '富华线缆2',
          },
          {
            id: 3,
            name: '富华线缆3',
          },
          {
            id: 4,
            name: '富华线缆4',
          },
          {
            id: 5,
            name: '富华线缆5',
          },
          {
            id: 6,
            name: '富华线缆6',
          },
          {
            id: 7,
            name: '富华线缆7',
          },
        ]
      },
      {
        id: 2,
        name: '管件管材2',
        itemList: [
          {
            id: 1,
            name: '富华线缆1',
          },
          {
            id: 2,
            name: '富华线缆2',
          },

        ]
      },
      {
        id: 3,
        name: '管件管材3',
        itemList: [
          {
            id: 1,
            name: '富华线缆1',
          },
          {
            id: 2,
            name: '富华线缆2',
          },
          {
            id: 3,
            name: '富华线缆3',
          },
          {
            id: 4,
            name: '富华线缆4',
          },
          {
            id: 5,
            name: '富华线缆5',
          },
        ]
      },
      {
        id: 4,
        name: '管件管材4',
        itemList: [
          {
            id: 1,
            name: '富华线缆1',
          },
          {
            id: 2,
            name: '富华线缆2',
          },
          {
            id: 3,
            name: '富华线缆3',
          },
          {
            id: 4,
            name: '富华线缆4',
          },
          {
            id: 5,
            name: '富华线缆5',
          },
        ]
      }
    ],
    pinpaiIndex: 1
  },
  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({ swiper, tabList }) => {
      this.setData({
        tabList,
        imgSrcs: swiper,
        pageLoading: false,
      });
      this.loadGoodsList(true);
    });
  },

  tabChangeHandle(e) {
    this.loadGoodsList(true);
  },

  onReTry() {
    // this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {

  },

  goodListClickHandle(e) {
    const { index } = e.detail;
    const { spuId } = this.data.goodsList[index];
    wx.navigateTo({
      url: `/pages/brand/details/index?spuId=${spuId}`,
    });
  },
  // 跳转分类详情页
  toDetail(e) {
    console.log('输出11', e.currentTarget.dataset)
    let item = e.currentTarget.dataset.item
    if (item.path == '/pages/brand/category/index') {
      wx.setStorage({
        key: "brandForm",
        data: item.type//用来处理跳转分类后.打开分类详情 定位到品牌故事还是最新报价1:品牌故事,2;最新报价
      })
      wx.switchTab({ url: '/pages/brand/category/index' });
    } else {
      wx.navigateTo({
        url: item.path,
      });
    }

  },
  // 资讯详情
  toItem(e) {
    console.log('输出', e.currentTarget.dataset)
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/informationDetail/informationDetail?id=${item.spuId}&type=1`,
    });

  },
  toMore() {
    console.log('资讯查看更多')
    wx.navigateTo({
      url: '/pages/newsList/newsList?type=1',
    });
  },
  // 品牌展示切换
  onTabsChange(e) {
    this.setData({ value: e.detail.value })
  },
  // 品牌详情
  topinpaiDetail(e) {
    wx.navigateTo({
      url: `/pages/brand/details/index?spuId=${e.currentTarget.dataset.spuId}`
    });
  },
  // 品牌更多
  topinpaiMore() {
    wx.setStorage({
      key: "brandForm",
      data: 1//用来处理跳转分类后.打开分类详情 定位到品牌故事还是最新报价1:品牌故事,2;最新报价
    })
    wx.switchTab({ url: '/pages/brand/category/index' });
  },
  goodListAddCartHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加入购物车',
    });
  },

  navToSearchPage() {
    // wx.navigateTo({ url: '/pages/brand/search/index' });
    wx.navigateTo({
      url: `/pages/brand/result/index`,
    });
  },

  navToActivityDetail({ detail }) {
    const item = detail || {};
    wx.navigateTo({
      url: `/pages/informationDetail/informationDetail?id=${item.spuId}&type=1`,
    });
  },
});
